﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.Objects;
using System.IO;
using Microsoft.Win32;
using ChoDiaOc_HK_WPF.Controls;
using ChoDiaOc_HK_WPF.Models;

namespace ChoDiaOc_HK_WPF.Views.ChupAnh {
    /// <summary>
    /// Interaction logic for DangAnh.xaml
    /// </summary>
    public partial class DangAnh : UserControl {
        public DangAnh() {
            InitializeComponent();
            entities = new DBEntities();
        }

        private DBEntities entities;

        private void btnNhapMa_Click(object sender, RoutedEventArgs e) {
            System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["pHIEUDANGKiesViewSource"];
            IQueryable<PHIEUDANGKY> q = entities.PHIEUDANGKies.Where(t => t.MaPhieuDangKy == maPhieuDangKyTextBox.Text);
            if (q.ToList().Count == 0) {
                MessageBox.Show("Không tồn tại phiếu đăng ký!", "Cảnh báo", MessageBoxButton.YesNo, MessageBoxImage.Warning);
                return;
            }
            myCollectionViewSource.Source = q;
            imgAnhChup.Source = null;
            DIAOC diaOc = q.FirstOrDefault().DIAOC;
            PHIEUDANGKY phieu = q.FirstOrDefault();
            QC_BAO qcBao = phieu.QC_BAO.FirstOrDefault();
            QC_BANG qcBang = phieu.QC_BANG.FirstOrDefault();
            TOBUOM qcBuom = phieu.TOBUOMs.FirstOrDefault();
            NOIDUNGQC noiDung = null;
            if (qcBang != null) {
                noiDung = qcBang.NOIDUNGQC;
            } else {
                if (qcBao != null) {
                    noiDung = qcBao.NOIDUNGQC;
                } else {
                    if (qcBuom != null) {
                        noiDung = qcBuom.NOIDUNGQC;
                    }
                }
            }
            if (noiDung != null) {
                NoiDungQC.MaND = noiDung.MaND;
            }

            System.Windows.Data.CollectionViewSource myCollectionViewSource2 = (System.Windows.Data.CollectionViewSource)this.Resources["hINHANHsViewSource"];
            myCollectionViewSource2.Source = entities.HINHANHs.Where(ha => ha.MaND == NoiDungQC.MaND);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e) {

        }

        private NOIDUNGQC noiDungQC;
        public ChoDiaOc_HK_WPF.NOIDUNGQC NoiDungQC {
            get {
                if (noiDungQC == null) {
                    noiDungQC = new NOIDUNGQC();
                }
                return noiDungQC;
            }
            set { noiDungQC = value; }
        }

        private void hINHANHsListView_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (hINHANHsListView.SelectedIndex != -1) {
                HINHANH ha = hINHANHsListView.SelectedItem as HINHANH;
                FileStream stream = new FileStream("./AnhChupDiaOc/" + ha.Anh, FileMode.Open, FileAccess.Read);
                Image i = new Image();
                BitmapImage src = new BitmapImage();
                src.BeginInit();
                src.StreamSource = stream;
                src.EndInit();
                imgAnhChup.Source = src;
                imgAnhChup.Stretch = Stretch.Fill;
            }
        }

        private void btnHoanTat_Click(object sender, RoutedEventArgs e) {
            try {
                entities.SaveChanges();
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            } catch (Exception exp) {
                MessageBox.Show(exp.Message);
            }
        }

        private void btnThem_Click(object sender, RoutedEventArgs e) {
            if (entities.HINHANHs.Where(ha32 => ha32.MaND == NoiDungQC.MaND).ToList().Count == DatabaseWrapper.GetInstance().GetThamSoSoLuongAnh()) {
                MessageBox.Show("Đã đủ ảnh theo quy định!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            HINHANH ha = new HINHANH();
            ha.MaHinhAnh = ChupAnhController.GetInstance().GenerateMaHINHANH();
            OpenFileDialog filedialog = new OpenFileDialog();
            filedialog.Filter = "*.png|*.png|*.jpg|*.jpg|*.bmp|*.bmp|*.ico|*.ico";
            filedialog.ShowDialog();
            FileInfo fi;
            if (filedialog.FileName != String.Empty) {
                fi = new FileInfo(filedialog.FileName);
                if (fi.Exists) {
                    ha.Anh = fi.Name;
                    ha.MaND = NoiDungQC.MaND;
                    ha.MoTa = "Ảnh chụp địa ốc";
                    ha.ThoiGianChupAnh = DateTime.Now;
                    FileInfo fiEx = new FileInfo("./AnhChupDiaOc/" + fi.Name);
                    if (fiEx.Exists){
                        return;
                    }
                    fi.CopyTo("./AnhChupDiaOc/" + fi.Name, true);
                    entities.HINHANHs.AddObject(ha);
                    entities.SaveChanges();
                    FileStream stream = new FileStream("./AnhChupDiaOc/" + ha.Anh, FileMode.Open, FileAccess.Read);
                    Image i = new Image();
                    BitmapImage src = new BitmapImage();
                    src.BeginInit();
                    src.StreamSource = stream;
                    src.EndInit();
                    imgAnhChup.Source = src;
                    imgAnhChup.Stretch = Stretch.Fill;
                    System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["hINHANHsViewSource"];
                    entities = new DBEntities();
                    myCollectionViewSource.Source = entities.HINHANHs.Where(ha2 => ha2.MaND == NoiDungQC.MaND);
                }
            }

        }

        private void btnXoa_Click(object sender, RoutedEventArgs e) {
            if (hINHANHsListView.SelectedIndex != -1) {
                HINHANH ha = hINHANHsListView.SelectedItem as HINHANH;
                if (MessageBoxResult.Yes == MessageBox.Show("Chắc chắn?", "Cảnh báo", MessageBoxButton.YesNo, MessageBoxImage.Question)) {
                    entities.HINHANHs.DeleteObject(ha);
                    entities.SaveChanges();
                    imgAnhChup.Source = null;
                    hINHANHsListView.SelectedIndex = -1;
                }
            }
        }
    }
}
